
/**
 * 初始化抓取到的信息
 * @param tiyData
 */
export const initCrawledInfo = (tiyData) => {
  tiyData.tStatus.modalAddByCrawler.isParsed = false;
  tiyData.tParam.crawler.sourceUrl = "";
  tiyData.tParam.crawler.title = "";
  tiyData.tParam.crawler.shippingFee = "";
  tiyData.tParam.crawler.colors.splice(0, tiyData.tParam.crawler.colors.length);
  tiyData.tParam.crawler.prices.splice(0, tiyData.tParam.crawler.prices.length);
  tiyData.tParam.crawler.attrs.splice(0, tiyData.tParam.crawler.attrs.length);
  tiyData.tParam.crawler.imgs.splice(0, tiyData.tParam.crawler.imgs.length);
  tiyData.tData.crawlerAttrs.splice(0, tiyData.tData.crawlerAttrs.length);
}

/**
 *
 * @param tiyData
 */
export const makeClassificationTree = (self, tiyData) =>{
  let classifications = self.$store.getters.product.classifications;
  let level1Resource = classifications.filter(category => category.level == 1);
  let level2Resource = classifications.filter(category => category.level == 2);
  let level3Resource = classifications.filter(category => category.level == 3);
  let dataResult = [];
  tiyData.tData.classificationTreeShow.splice(0,tiyData.tData.classificationTreeShow.length);
  tiyData.tData.classificationTree.splice(0, tiyData.tData.classificationTree.length);
  level1Resource.forEach(function(classificationLevel1){
    let dataLevel2 = [];
    level2Resource.filter(classification => classification.parentId == classificationLevel1.id).forEach(function (itemLevel2) {
      let dataLevel3 = [];
      level3Resource.filter(classification => classification.parentId == itemLevel2.id).forEach(function (itemLevel3) {
        let item = {value: itemLevel3.id, title: itemLevel3.name,level:3};

        if(tiyData.tParam.paramProduct.classificationIds.length!=0&&tiyData.tParam.paramProduct.classificationIds.indexOf(itemLevel3.id)!=-1){
          item.checked = true;
          tiyData.tData.classificationTreeShow.push({"level1":classificationLevel1.name,"level2":itemLevel2.name,"level3":itemLevel3.name});
        }
        dataLevel3.push(item);
      });
      dataLevel2.push({value: itemLevel2.id, title: itemLevel2.name,children: dataLevel3 ,level:2});

    });
    tiyData.tData.classificationTree.push({value:classificationLevel1.id,title:classificationLevel1.name,children:dataLevel2,level:1});
  });
}

export const toAddProductByCrawler = (self, tiyData) => {
  tiyData.tParam.paramProduct.productCode = '';
  tiyData.tParam.paramProduct.productName = '';
  tiyData.tParam.paramProduct.classificationIds = [];
  tiyData.tParam.paramProduct.sysSizeType='';
  tiyData.tParam.paramProduct.productState = -1;
  tiyData.tData.carwlerData = "";
  initCrawledInfo(tiyData);
  makeClassificationTree(self, tiyData);
}

/**
 * 抓取到产品信息保存到后台
 * @param self
 * @param tiyData
 */
export const addProductByCrawler = (self, tiyData) => {
  if(!tiyData.tParam.crawler){
    self.$Modal.error({
      title: "error",
      content: "没有抓取信息",
      width:500
    });
    return;
  }
  self.$Spin.show();
  let data = {
    "code":tiyData.tParam.paramProduct.productCode,
    "classificationIds":tiyData.tParam.paramProduct.classificationIds.toString(),
    "sysSizeType":tiyData.tParam.paramProduct.sysSizeType,
    "crawler":tiyData.tParam.crawler
  };

  self.$apiProduct.addProductByCrawler(data).then(function (response) {
    self.$Spin.hide();
    if(response.success){
      self.$Message.success('保存成功!可以继续编辑产品！');
      tiyData.tStatus.modalAddByCrawler.isShow = false;
      self.toEditProduct(response.result);
    } else {
      self.$Message.error(response.errorMessage);
    }
  });
}

/**
 * 保存成功后展示编辑产品页面
 * @param product
 * @param tiyData
 */
export const editProduct = (self, product, tiyData) => {
    tiyData.tParam.productTabs = 'product';
    let productArgs = product.arg;
    let productColors = product.color;
    let sysSizes = product.productSize;
    let productPrices = product.productPrice;

    //色卡
    tiyData.tData.productColorList.splice(0,tiyData.tData.productColorList.length);
    if(productColors.length>0){
      productColors.forEach(function(item){
        tiyData.tData.productColorList.push(item)
      })
    }

    //分类
    let productClassifications = product.productClassifications;
    tiyData.tParam.paramProduct.classificationIds.splice(0, tiyData.tParam.paramProduct.classificationIds.length);
    if (null != productClassifications) {
      productClassifications.forEach(function (item) {
        tiyData.tParam.paramProduct.classificationIds.push(item.classificationId);
      });
    }

    //价格
    tiyData.tParam.paramProduct.priceList.length=0;
    productPrices.forEach(function(item){
      tiyData.tParam.paramProduct.priceList.push({'min':item.min,'price':item.price});
    })

    //编辑页面表单数据
    tiyData.tStatus.modalModify.isEditDetail = false;
    tiyData.tParam.paramProduct.id = product.id;
    tiyData.tParam.paramProduct.productCode = product.code;
    tiyData.tParam.paramProduct.productName = product.name;
    tiyData.tParam.paramProduct.listImgUrl = product.listImgUrl;
    tiyData.tParam.paramProduct.productWeight = product.weight;
    tiyData.tParam.paramProduct.productMaterial = product.material;
    tiyData.tParam.paramProduct.sequence = product.sequence;
    tiyData.tParam.paramProduct.productState = product.state;
    tiyData.tParam.paramProduct.productIsUnisex = product.isUnisex;
    tiyData.tParam.paramProduct.productGender = product.gender;
    tiyData.tParam.paramProduct.shelves = product.shelves;
    tiyData.tParam.paramProduct.priceUnit=product.priceUnit;
    tiyData.tParam.paramProduct.keyword = product.keyword;

    //尺码相关
    tiyData.tParam.paramProduct.sysSizeType = product.sizeType;
    tiyData.tParam.sysSizeTypeChange = 0;
    tiyData.tParam.productSizeTable = {};
    tiyData.tData.productSizeDetail.splice(0, tiyData.tData.productSizeDetail.length);
    tiyData.tParam.paramProduct.productSizeImages.splice(0, tiyData.tParam.paramProduct.productSizeImages.length);
    tiyData.tParam.paramProduct.sysSizeIds.splice(0, tiyData.tParam.paramProduct.sysSizeIds.length);
    tiyData.tParam.paramProduct.customSize.splice(0, tiyData.tParam.paramProduct.customSize.length);
    sysSizes.forEach(function(item){
      if(item.selfDesignSize){ //是否为自定义尺码
        tiyData.tParam.paramProduct.customSize.push({"name":item.name,"sequence":item.sequence});
      } else {
        tiyData.tParam.paramProduct.sysSizeIds.push(item.sysSizeId);
      }
    })

    //参数
    tiyData.tParam.paramProduct.argJson.splice(0, tiyData.tParam.paramProduct.argJson.length);
    productArgs.forEach(function (item) {
      tiyData.tParam.paramProduct.argJson.push({name: item.paramName, value: item.paramValue});
    });

    //构造分类树
    makeClassificationTree(self, tiyData);

    tiyData.tStatus.modalModify.isShow = true;
}
